package com.hpq.user.service.handler;

import cn.hutool.json.JSONUtil;
import com.hpq.chat.domain.model.WxMsg;
import com.hpq.user.service.adapter.TextBuilder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author hpq
 * @description:消息处理器
 */
@Component
public class MsgHandler extends AbstractHandler{
//    @Autowired
//    private WxMsgDao wxMsgDao;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        if (true) {
            WxMsg msg = new WxMsg();
            msg.setOpenId(wxMpXmlMessage.getFromUser());
            msg.setMsg(wxMpXmlMessage.getContent());
//            wxMsgDao.save(msg);
            return null;
        }
        if (!wxMpXmlMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT)) {
            //可以选择将消息保存到本地
        }

        //当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
        try {
            if (StringUtils.startsWithAny(wxMpXmlMessage.getContent(), "你好", "客服")
                    && !wxMpService.getKefuService().kfOnlineList()
                    .getKfOnlineList().isEmpty()) {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
                        .fromUser(wxMpXmlMessage.getToUser())
                        .toUser(wxMpXmlMessage.getFromUser()).build();
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        //组装回复消息
        String content = "收到信息内容：" + JSONUtil.toJsonStr(wxMpXmlMessage);

        return TextBuilder.build(wxMpXmlMessage,content);
    }
}
